package com.yuwei.play.adapter

import android.app.AlertDialog
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.common.util.concurrent.ClosingFuture.Combiner4
import com.yuwei.play.R
import com.yuwei.play.entity.CommentEntity
import com.yuwei.play.entity.CommentEntityItem
import com.yuwei.play.entity.Reply
import com.yuwei.play.mvi.PlayIntent

class CommentAdapter : BaseQuickAdapter<CommentEntityItem, BaseViewHolder>(R.layout.comment_item) {

    private var position = 0
    fun sendPosition(index: Int) {
        this.position = index
    }

    override fun convert(holder: BaseViewHolder, item: CommentEntityItem) {
        val iv = holder.getView<ImageView>(R.id.commit_iv)
        Glide.with(context)
            .load(if (item.userlogo.isNotEmpty()) item.userlogo else R.drawable.sunrise)
            .circleCrop().into(iv)
        holder.setText(
            R.id.comment_name,
            if (item.nickname.isNotEmpty()) item.nickname else "最帅黑大帅"
        )
        holder.setText(
            R.id.comment_content,
            if (item.content.isNotEmpty()) item.content else "高考加油！！！"
        )
        val commentTwoAdapter = CommentTwoAdapter()
        val more = holder.getView<LinearLayout>(R.id.comment_more)
        if (item.replyList.isNotEmpty()) {
            val rv = holder.getView<RecyclerView>(R.id.comment_two_list)
            more.visibility = View.VISIBLE

            if (rv.adapter == null) {
                rv.layoutManager = LinearLayoutManager(context)
                rv.adapter = commentTwoAdapter
            }

            more.setOnClickListener {
                // 遍历所有数据，重置展开状态
                data.forEach { it.isExpanded = false }
                // 仅当前项展开
                item.isExpanded = true
                // 刷新整个列表
               notifyDataSetChanged()

            }
// 根据 isExpanded 显示/隐藏二级列表
            rv.visibility = if (item.isExpanded) View.VISIBLE else View.GONE
            more.visibility = if (item.isExpanded) View.GONE else View.VISIBLE
            commentTwoAdapter.setList(item.replyList)

        }

    }
}